/*
** convert_base.c for piscine Jour_08 in /u/all/cauret_l/rendu/piscine/Jour_08/ex_02
** 
** Made by louis cauret
** Login   <cauret_l@epitech.net>
** 
** Started on  Thu Oct 11 09:46:24 2012 louis cauret
** Last update Thu Oct 11 20:09:56 2012 louis cauret
*/

#include <stdlib.h>

char		*convert_func(int nb, char *base, int i, int count2)
{
  int		count;
  char		*str;

  while (base[i] != '\0')
    {
      i = i + 1;
    }
  count = i;
  while ((nb / i) > 0)
    {
      i = i * count;
    }
  str = malloc(sizeof(*str) + count);
  while (i > count)
    {
      i = i / count;
      str[count2] = base[(nb / i) % count];
      count2 = count2 + 1;
    }
  str[count2] = (base[nb % count]);
  str[count2 + 1] = '\0';
  return (str);
}

char		*convert_base(char *nbr, char *base_from, char *base_to)
{
  int		i;
  int		j;
  int		nb;
  int		base;

  i = 0;
  j = 0;
  nb = 0;
  base = 0;
  while (base_from[base] != '\0')
    {
      base = base + 1;
    }
  while (nbr[j] != '\0')
    {
      while (nbr[j] != base_from[i])
	{
	  i = i + 1;
	}
      nb = nb * base + i;
      j = j + 1;
      i = 0;
    }
  return (convert_func(nb, base_to, 0, 0));
}
